﻿using HarmonyLib;
using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;

namespace SuperMe
{
    public class SuperMeSettings : ModSettings
    {
        public  List<ModifyInfo> default_stat_defs;
        private  Dictionary<string, StatDef> dict_name_stat_def;
        public SuperMeSettings()
        {
            InitDef();
        }
        public void InitDef()
        {
            default_stat_defs = new List<ModifyInfo>();
            default_stat_defs.Add(new ModifyInfo("GlobalLearningFactor", 100.0f));
            default_stat_defs.Add(new ModifyInfo("WorkTableWorkSpeedFactor", 13.0f));
            default_stat_defs.Add(new ModifyInfo("WorkTableEfficiencyFactor", 2.0f));
            default_stat_defs.Add(new ModifyInfo("ResearchSpeedFactor", 11.0f));
            default_stat_defs.Add(new ModifyInfo("ConstructionSpeedFactor", 10.0f));

            default_stat_defs.Add(new ModifyInfo("WorkSpeedGlobal", 10.0f));
            default_stat_defs.Add(new ModifyInfo("CarryingCapacity", 50.0f));
            default_stat_defs.Add(new ModifyInfo("DoorOpenSpeed", 10.0f));
            default_stat_defs.Add(new ModifyInfo("EatingSpeed", 10.0f));
            default_stat_defs.Add(new ModifyInfo("SmoothingSpeed", 20.0f));

            default_stat_defs.Add(new ModifyInfo("MiningSpeed", 10.0f));
            default_stat_defs.Add(new ModifyInfo("PlantWorkSpeed", 10.0f));
            default_stat_defs.Add(new ModifyInfo("PlantHarvestYield", 5.0f));

            default_stat_defs.Add(new ModifyInfo("SellPriceFactor", 4.0f));
        }

        public  void LoadDef()
        {
            SuperMeDef.Log("SuperMe SuperMeSettings LoadDef.");

            dict_name_stat_def = new Dictionary<string, StatDef>();
            dict_name_stat_def.Add("GlobalLearningFactor", StatDefOf.GlobalLearningFactor);

            dict_name_stat_def.Add("WorkTableWorkSpeedFactor", StatDefOf.WorkTableWorkSpeedFactor);
            dict_name_stat_def.Add("WorkTableEfficiencyFactor", StatDefOf.WorkTableEfficiencyFactor);
            dict_name_stat_def.Add("ResearchSpeedFactor", StatDefOf.ResearchSpeedFactor);
            dict_name_stat_def.Add("ConstructionSpeedFactor", StatDefOf.ConstructionSpeedFactor);

            dict_name_stat_def.Add("WorkSpeedGlobal", StatDefOf.WorkSpeedGlobal);
            dict_name_stat_def.Add("CarryingCapacity", StatDefOf.CarryingCapacity);
            dict_name_stat_def.Add("DoorOpenSpeed", StatDefOf.DoorOpenSpeed);
            dict_name_stat_def.Add("EatingSpeed", StatDefOf.EatingSpeed);
            dict_name_stat_def.Add("SmoothingSpeed", StatDefOf.SmoothingSpeed);

            dict_name_stat_def.Add("MiningSpeed", StatDefOf.MiningSpeed);
            dict_name_stat_def.Add("PlantWorkSpeed", StatDefOf.PlantWorkSpeed);
            dict_name_stat_def.Add("PlantHarvestYield", StatDefOf.PlantHarvestYield);

            dict_name_stat_def.Add("SellPriceFactor", StatDefOf.SellPriceFactor);

            foreach (ModifyInfo mi in default_stat_defs)
            {
                if (dict_name_stat_def.ContainsKey(mi.stat_def_name))
                {
                    mi.stat_def = dict_name_stat_def[mi.stat_def_name];
                    SuperMeDef.Log("SuperMe SuperMeSettings LoadDef Make Pair:" + mi.stat_def_name + " - " + dict_name_stat_def[mi.stat_def_name].defName + " - " + dict_name_stat_def[mi.stat_def_name].description);
                }
            }
        }

        public override void ExposeData()
        {
            SuperMeDef.Log("SuperMe SuperMeSettings Do ExposeData. Scribe.mode:" + Scribe.mode.ToString()+ " default_stat_defs:size:"+ default_stat_defs.Count());
            foreach (ModifyInfo mi in default_stat_defs)
            {
                string label = mi.stat_def_name;
                Scribe_Values.Look<float>(ref mi.factor, label, mi.default_factor, false);
            }
        }
        private  Vector2 infoScrollPosition = Vector2.zero;

        internal  void DoSettingsWindowContents(Rect inRect)
        {
            SuperMeDef.Log("SuperMe SuperMeSettings DoSettingsWindowContents.");
            Listing_Standard listing_Standard = new Listing_Standard(GameFont.Small);
            Rect outRect = new Rect(0f, 40f, inRect.width, inRect.height - 30f);
            Rect viewRect = new Rect(0f, 0f, inRect.width - 16f, inRect.height + 500f);
            listing_Standard.BeginScrollView(outRect, ref infoScrollPosition, ref viewRect);
            foreach (ModifyInfo mi in default_stat_defs)
            {
                string labe_trans = "ScenPart_StatFactor".Translate(mi.stat_def_name, mi.factor.ToStringPercent());
                listing_Standard.Label(labe_trans);
                mi.factor = listing_Standard.Slider(mi.factor, 1.0f, 100.0f);
            }
            // 如果按下按钮
            if (listing_Standard.ButtonTextLabeled("Setting:", "Print stat_def_name."))
            {
                SuperMeDef.Log("SuperMe SuperMeSettings Save Setting got Called.");
                FileLog.Log("SuperMe SuperMeSettings Save Setting got Called.");
                foreach (ModifyInfo mi in default_stat_defs)
                {
                    if (mi.stat_def == null)
                    {
                        FileLog.Log(mi.stat_def_name);
                    }
                    else
                    {
                        FileLog.Log(mi.stat_def.defName + "   -   " + mi.stat_def.description);
                    }
                }
            }
            if (listing_Standard.ButtonTextLabeled("Save","Confirm Save"))
            {
                SuperMeDef.Log("SuperMe SuperMeSettings Confirm Save.");
                this.Write();
            }
            listing_Standard.EndScrollView(ref viewRect);

        }
    }
}
